package edu.hawaii.webspider;

import org.junit.Test;

import static org.junit.Assert.assertSame;

/**
 *   Tests functions of the WebAddressDatabase class.
 *   
 *   @author Andrew Wong
 */
public class TestWebAddressDatabase {
  static final String googleUrl = "http://www.google.com";
  static final String hackystatUrl = "http://www.hackystat.org";
  static final String uhUrl = "http://www.hawaii.edu";

  /**
   *  Tests to see if adding and removing URLs is ok.
   */
  @Test
  public void testAddAndDelete() {
    WebAddressDatabase test = new WebAddressDatabase();
    int count;
    
    test.addUrl(googleUrl);
    test.addUrl(googleUrl);
    test.addUrl(hackystatUrl);
    
    count = test.getCount(googleUrl);    
    assertSame("Google URL was added twice", count, 2);
    
    count = test.getCount(hackystatUrl);    
    assertSame("Hackystat URL was added once", count, 1);

    count = test.getCount(uhUrl);    
    assertSame("UH url was not added", count, 0);
  }
  
  /**
   * Tests for the visited property of URLs in the database.
   */
  @Test
  public void testVisitedUrls() {
    WebAddressDatabase test = new WebAddressDatabase();
    
    test.addUrl(googleUrl);
    test.addUrl(googleUrl);
    test.addUrl("http://www.hackystat.org");
    
    test.markUrl(googleUrl);
    test.markUrl("http://www.hawaii.edu");
    
    assertSame("Google URL should be marked visited", true, 
        test.visited(googleUrl));
    assertSame("Hawaii.edu URL should be marked visited", true, 
        test.visited("http://www.hawaii.edu"));
    assertSame("Hackystat URL should NOT be marked visited", false, 
        test.visited("http://www.hackystat.org"));
    assertSame("Myspace URL should NOT be marked visited", false, 
        test.visited("http://www.myspace.com"));
  }
  
  /**
   * Checks that URLs can be retrieved as well as added.
   */
  @Test
  public void testTraversal() {
    WebAddressDatabase test = new WebAddressDatabase();
    
    test.addUrl(googleUrl);
    test.addUrl(uhUrl);
    test.addUrl(hackystatUrl);
    
    System.out.println("URLs:");
    
    String current;
    
    current = test.getNextUnvisited();
    assertSame("Google should be first to come out", googleUrl, current);
    
    current = test.getNextUnvisited();
    assertSame("UH URL expected", uhUrl, current);

    current = test.getNextUnvisited();
    assertSame("Hackystat URL expected", hackystatUrl, current);

    current = test.getNextUnvisited();
    assertSame("no url expected", null, current);
  }
}
